﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfControlDemo.逻辑树与视觉树
{
    /// <summary>
    /// TwoTreesWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TwoTreesWindow : Window
    {
        public TwoTreesWindow()
        {
            InitializeComponent();
        }

        #region 显示逻辑树
        /// <summary>
        /// 遍历到的元素添加到TreeView中
        /// </summary>
        /// <param name="obj">逻辑树对象</param>
        /// <param name="item">treeView项</param>
        /// <param name="parentItem">treeView父节点</param>
        private void AddTreeViewItem(object obj, TreeViewItem item, TreeViewItem parentItem)
        {
            // 设置目录树标题
            item.Header = obj.GetType().Name; // 获取逻辑树对象名称
            item.IsExpanded = true;  // 设置标题展开
            if (parentItem == null) 
            {
                tvTrees.Items.Add(item); // 目录树添加首个节点
            }
            else
            {
                parentItem.Items.Add(item); // 添加子节点
            }
        }

        /// <summary>
        /// 遍历逻辑树方法
        /// </summary>
        /// <param name="obj">逻辑树对象</param>
        /// <param name="parentItem">目录树父节点</param>
        private void PrintLogcTree(object obj, TreeViewItem parentItem)
        {
            TreeViewItem item = new TreeViewItem(); // 实例化目录树节点
            AddTreeViewItem(obj, item, parentItem); // 添加目录树节点
            if (!(obj is DependencyObject)) // 依赖属性对象
            {
                return;
            }
            // 循环对象的子节点  -  LogicalTreeHelper.GetChildren(obj as DependencyObject) 返回对象的子对象集合
            foreach (object child in LogicalTreeHelper.GetChildren(obj as DependencyObject))
            {
                if (child is TreeView) // 不遍历目录树控件
                    return;
                PrintLogcTree(child, item); // 递归循环
            }
        }

        /// <summary>
        /// 显示逻辑树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShowLogicTree(object sender, RoutedEventArgs e)
        {
            tvTrees.Items.Clear(); // 清空目录树项
            PrintLogcTree(this, null); // 递归显示逻辑树
        }
        #endregion

        #region 显示视觉树
        /// <summary>
        /// 遍历视觉树
        /// </summary>
        /// <param name="obj">视觉树对象</param>
        /// <param name="parentItem">目录树父类</param>
        private void PrintVisualTree(DependencyObject obj, TreeViewItem parentItem)
        {
            TreeViewItem item = new TreeViewItem(); // 目录树节点
            AddTreeViewItem(obj, item, parentItem); // 添加目录树节点
            // 循环视觉树集合  -  VisualTreeHelper.GetChildrenCount(obj)-获取视觉树集合方法
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                if (obj is TreeView) // 不遍历目录树控件
                    return;
                PrintVisualTree(VisualTreeHelper.GetChild(obj, i), item); // 递归循环视觉树
            }
        }

        /// <summary>
        /// 显示视觉树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShowVisualTree(object sender, RoutedEventArgs e)
        {
            tvTrees.Items.Clear(); // 清空目录树项
            PrintVisualTree(this, null); // 递归显示逻辑树
        }
        #endregion
    }
}
